package com.woniuxy.hospital.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.woniuxy.hospital.entity.HplPatient;
import com.woniuxy.hospital.hplutils.DataJudgmentUtil;
import com.woniuxy.hospital.hplutils.ResponseEntity;
import com.woniuxy.hospital.service.IHplPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob、liaoxiangyun：2021-08-12登陆注册功能
 * @since 2021-08-12、
 */
@RestController
@RequestMapping("/patient")
public class HplPatientController {
    @Autowired
    private IHplPatientService patientService;
    //患者登陆
    @PostMapping("login")
    public ResponseEntity<Void> login(HplPatient patient){
        HplPatient hplPatient= patientService.getByAccount(patient.getAccount());
        if (hplPatient!=null && hplPatient.getPassword().equals(patient.getPassword())){
            return ResponseEntity.SUCCESS;//登陆成功，返回状态码200
        }else if (hplPatient==null){
            return ResponseEntity.FAIL;//登陆失败，账号不存在，返回状态码500
        }else {
            return ResponseEntity.INVALIDARGS;//登陆失败，密码不正确，返回状态码600
        }
    }

    //患者注册
    @PostMapping("register")
    public ResponseEntity<Void> register(HplPatient patient){
        String account=patient.getAccount();
        String password=patient.getPassword();
        String name=patient.getName();
        String sex = patient.getSex();
        String idCard=patient.getIdCard();
        String telephone=patient.getTelephone();
        String emergencyContact=patient.getEmergencyContact();
        String emergencyTel=patient.getEmergencyTel();
        //校验数据，无效参数返回状态码600，完成校验注册成功返回状态码200
        if(!DataJudgmentUtil.StringJudgment(account,6,12)){
            return ResponseEntity.INVALIDARGS;
        }else if (!DataJudgmentUtil.StringJudgment(password,6,12)){
            return ResponseEntity.INVALIDARGS;
        }else if (!DataJudgmentUtil.StringJudgment(name,2,10)){
            return ResponseEntity.INVALIDARGS;
        }else if (!DataJudgmentUtil.StringJudgment(sex,1,1)){
            return ResponseEntity.INVALIDARGS;
        }else if (!DataJudgmentUtil.StringJudgment(idCard,18,18)){
            return ResponseEntity.INVALIDARGS;
        }else if (!DataJudgmentUtil.StringJudgment(telephone,11,11)){
            return ResponseEntity.INVALIDARGS;
        }else if (!DataJudgmentUtil.StringJudgment(emergencyContact,2,10)){
            return ResponseEntity.INVALIDARGS;
        }else if (!DataJudgmentUtil.StringJudgment(emergencyTel,11,11)){
            return ResponseEntity.INVALIDARGS;
        }else {
            patient.setStatus("y");
            patientService.save(patient);
            return ResponseEntity.SUCCESS;
        }
    }

}

